﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using KerriganService.ServiceClient;
using KerriganInterface;
using KerriganService;
using System.ServiceModel.Description;
using System.IO;
using System.ServiceModel.Web;

namespace KerriganServer
{
    public partial class ServerForm : Form
    {
        ServiceHost hostBlogService;
        ServiceHost hostAgencyService;
        ServiceHost hostDirectionService;
        ServiceHost hostForumService;
        ServiceHost hostClientService;
        ServiceHost hostMobileService;


        public ServerForm()
        {
            InitializeComponent();
            bttStart_Click(null, null);
            this.tbLogServerStatus.Multiline = true;
        }

        private void bttStart_Click(object sender, EventArgs e)
        {
            StartServices();
            this.bttStart.Enabled = false;
        }

        private void bttStop_Click(object sender, EventArgs e)
        {
            this.bttStart.Enabled = true;
            this.bttStop.Enabled = false;

            hostBlogService.Close();
            hostDirectionService.Close();
            hostAgencyService.Close();
            hostForumService.Close();
            hostMobileService.Close();
            hostClientService.Close();
        }

        public void StartServices()
        {
            try
            {
                //Instanciation des services Host
                hostBlogService = new ServiceHost(typeof(BlogService));
                hostDirectionService = new ServiceHost(typeof(DirectionService));
                hostAgencyService = new ServiceHost(typeof(AgencyService));
                hostForumService = new ServiceHost(typeof(ForumService));
                hostMobileService = new ServiceHost(typeof(MobileService));
                hostClientService = new ServiceHost(typeof(ClientService));

                hostAgencyService.Opened += new EventHandler(hostAgencyService_Opened);
                hostBlogService.Opened += new EventHandler(hostBlogService_Opened);
                hostClientService.Opened += new EventHandler(hostClientService_Opened);
                hostDirectionService.Opened += new EventHandler(hostDirectionService_Opened);
                hostForumService.Opened += new EventHandler(hostForumService_Opened);
                hostMobileService.Opened += new EventHandler(hostMobileService_Opened);
                //Ouverture des services.

                hostBlogService.Open();
                hostDirectionService.Open();
                hostAgencyService.Open();
                hostForumService.Open();
                hostMobileService.Open();
                hostClientService.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " " + ex.StackTrace, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void hostMobileService_Opened(object sender, EventArgs e)
        {
            this.tbLogServerStatus.Text += "Mobile Service Status : " + (sender as ServiceHost).State.ToString() + "\r\n";
        }

        void hostForumService_Opened(object sender, EventArgs e)
        {
            this.tbLogServerStatus.Text += "Forum Service Status : " + (sender as ServiceHost).State.ToString() + "\r\n";
        }

        void hostDirectionService_Opened(object sender, EventArgs e)
        {
            this.tbLogServerStatus.Text += "Direction Service Status : " + (sender as ServiceHost).State.ToString() + "\r\n";
        }

        void hostClientService_Opened(object sender, EventArgs e)
        {
            this.tbLogServerStatus.Text += "Client Service Status : " + (sender as ServiceHost).State.ToString() + "\r\n";
        }

        void hostBlogService_Opened(object sender, EventArgs e)
        {
            this.tbLogServerStatus.Text += "Blog Service Status : " + (sender as ServiceHost).State.ToString() + "\r\n";
        }

        void hostAgencyService_Opened(object sender, EventArgs e)
        {
            this.tbLogServerStatus.Text += "Agency Service Status : " + (sender as ServiceHost).State.ToString() + "\r\n";
        }
    }
}
